package com.xiaoyu.lanling.feature.vip.activity

import android.os.Bundle
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.pay.PreparePayEvent
import com.xiaoyu.lanling.event.vip.VipTypeUpdateEvent
import com.xiaoyu.lanling.feature.pay.data.PayData
import com.xiaoyu.lanling.feature.vip.fragment.VipBuySuccessDialog
import com.xiaoyu.lanling.feature.vip.model.ProductItem
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.pay.ALI_PAY
import com.xiaoyu.pay.PayUtil
import com.xiaoyu.pay.PayWay
import com.xiaoyu.pay.WE_CHAT_PAY
import kotlinx.android.synthetic.main.vip_buy_activity.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VipBuyActivity : AppCompatToolbarActivity() {

    private val requestTag = Any()
    private val payUtil = PayUtil()

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.vip_buy_activity)
        initToolbar()

        initView()
        initBind()
        initData()
        initEvent()
    }

    private fun initView() {
        title = getString(R.string.vip_buy_title)
    }

    private fun initBind() {
        ali_pay_radio_check.setOnClickDebounceListener { updatePayWayCheck(ALI_PAY) }
        wechat_pay_radio_check.setOnClickDebounceListener { updatePayWayCheck(WE_CHAT_PAY) }
        button.setOnClickDebounceListener {
            val selectedProductItem = (intent.getSerializableExtra(Constant.IntentKey.KEY_VIP_PRODUCT_SELECTED) as? ProductItem)
                    ?: return@setOnClickDebounceListener
            val from = intent.getStringExtra(Constant.PARAM_FROM)
            when {
                ali_pay_radio_check.isSelected -> PayData.preparePay(requestTag, selectedProductItem.id, ALI_PAY, from)
                wechat_pay_radio_check.isSelected -> PayData.preparePay(requestTag, selectedProductItem.id, WE_CHAT_PAY, from)
            }
        }
    }

    private fun initData() {
        val selectedProductItem = (intent.getSerializableExtra(Constant.IntentKey.KEY_VIP_PRODUCT_SELECTED) as? ProductItem)
                ?: return
        showData(selectedProductItem)
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: PreparePayEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                onPay(event)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: VipTypeUpdateEvent) {
                if (event.oldType.isEmpty() && event.newType.isNotEmpty()) {
                    showVipTypeUpdateDialog()
                }
            }
        })
    }

    private fun showVipTypeUpdateDialog() {
        if (isDestroyed) {
            return
        }
        VipBuySuccessDialog.show(supportFragmentManager)
    }

    private fun onPay(event: PreparePayEvent) {
        when (event.payWay) {
            ALI_PAY -> payUtil.toAliPay(event.params)
            WE_CHAT_PAY -> payUtil.toWeChatPay(event.params)
        }
    }

    private fun showData(selectedProductItem: ProductItem) {
        subject.text = selectedProductItem.subject
        duration.text = selectedProductItem.duration
        price.text = getString(R.string.vip_buy_price_prefix, selectedProductItem.price)
        ImageViewUtil.loadImage(icon, ImageLoadParam.newBuilder().setEnableEmptyUrl(true).setUrl(selectedProductItem.vipInfo.icon).setSizeInDP(132).build())
    }

    private fun updatePayWayCheck(@PayWay payWay: String) {
        ali_pay_radio_check.isSelected = payWay == ALI_PAY
        wechat_pay_radio_check.isSelected = payWay == WE_CHAT_PAY
        button.isEnabled = true
    }
}